/* global wx */

// --创建页面js加载微信jsddk--
let isLoad = true
const sc = document.createElement('script')
sc.type = 'text/javascript'

// --微信功能--
export default {
  loadJsdk: function (wxconfig, callback) {
    const _this = this
    if (isLoad) {
      if (sc.readyState) {
        sc.onreadystatechange = function () {
          if (this.readyState === 'loaded' || this.readyState === 'complete') {
            _this.setWXconfig(wxconfig, callback)
            sc.onreadystatechange = null
            isLoad = false
          }
        }
      } else {
        sc.onload = function () {
          _this.setWXconfig(wxconfig, callback)
          sc.onload = null
          isLoad = false
        }
      }
      sc.src = 'http://res.wx.qq.com/open/js/jweixin-1.4.0.js'
      document.getElementsByTagName('head')[0].appendChild(sc)
    } else {
      _this.setWXconfig(wxconfig, callback);
    }
  },
  setWXconfig: function (wxconfig, callback) {
    const _this = this;
    wx.config({
      debug: wxconfig.debug,
      appId: wxconfig.appId,
      timestamp: wxconfig.timestamp,
      nonceStr: wxconfig.nonceStr,
      signature: wxconfig.signature,
      jsApiList: [
        // ----
        'onMenuShareAppMessage',
        'onMenuShareTimeline',
        'onMenuShareQQ',
        'onMenuShareQZone',
        // ----
        'updateAppMessageShareData',
        'updateTimelineShareData',
        'onMenuShareWeibo',
        // ----
        'chooseImage',
        'getLocalImgData',
        // ----
        'hideAllNonBaseMenuItem',
        'showMenuItems'
      ]
    })
    wx.ready(function () {
      callback && callback(_this);
    })
  },
  showMenuList: function (showItem) {
    wx.showMenuItems({ menuList: showItem })
  },
  hideMenuItem: function () {
    wx.hideAllNonBaseMenuItem()
  },
  share: function (shareinfo) {
    // ----
    wx.onMenuShareAppMessage({
      title: shareinfo.title,
      desc: shareinfo.desc,
      link: shareinfo.link,
      imgUrl: shareinfo.imgUrl
    })
    wx.onMenuShareTimeline({
      title: shareinfo.title,
      link: shareinfo.link,
      imgUrl: shareinfo.imgUrl
    })
    wx.onMenuShareQQ({
      title: shareinfo.title,
      desc: shareinfo.desc,
      link: shareinfo.link,
      imgUrl: shareinfo.imgUrl
    })
    wx.onMenuShareQZone({
      title: shareinfo.title,
      desc: shareinfo.desc,
      link: shareinfo.link,
      imgUrl: shareinfo.imgUrl
    })
    // --自定义“分享给朋友”及“分享到QQ”按钮的分享内容--
    wx.updateAppMessageShareData({
      title: shareinfo.title,
      desc: shareinfo.desc,
      link: shareinfo.link,
      imgUrl: shareinfo.imgUrl
    })
    // --自定义“分享到朋友圈”及“分享到QQ空间”按钮的分享内容--
    wx.updateTimelineShareData({
      title: shareinfo.title,
      desc: shareinfo.desc,
      link: shareinfo.link,
      imgUrl: shareinfo.imgUrl
    })
    // --获取“分享到腾讯微博”按钮点击状态及自定义分享内容--
    wx.onMenuShareWeibo({
      title: shareinfo.title,
      desc: shareinfo.desc,
      link: shareinfo.link,
      imgUrl: shareinfo.imgUrl
    })
  },
  selectImage: function (choose, callback) {
    wx.chooseImage({
      count: choose.num,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        const arr = res.localIds
        const len = arr.length
        let k = 0
        const newItems = []
        const getSelfDate = (item) => {
          if (k < len) {
            wx.getLocalImgData({
              localId: item,
              success: function (curRes) {
                let forLocalData = curRes.localData
                if (Number(forLocalData.indexOf('data:image')) === Number(-1)) {
                  forLocalData = 'data:image/jpeg;base64,' + forLocalData
                }
                forLocalData = forLocalData.replace(/\r|\n/g, '')
                const forExtend = forLocalData.split(';base64,')[0].split('data:image/')[1]
                newItems.push({
                  url: forLocalData,
                  ext: forExtend
                })
                k++
                getSelfDate(arr[k])
              }
            })
          } else {
            callback && callback(newItems)
          }
        }
        getSelfDate(arr[0])
      }
    })
  }
}
